package com.atguigu.order.service.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.order.bean.Order;
import com.atguigu.order.feign.ProductFeignClient;
import com.atguigu.order.service.OrderService;
import com.atguigu.product.bean.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ProductFeignClient productFeignClient;


    //兜底方法
    @SentinelResource(value = "createOrder" , blockHandler = "createOrderFallBack")
    @Override
    public Order createOrder(Long productId, Long userId) {
       // Product product = getProductFromRemoteAnnotation(productId);
        Product product = productFeignClient.getProduct(productId);
        Order order = new Order();
        order.setId(1L);
        //总金额
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));

        order.setUserId(userId);
        order.setNickName("zhangsan");
        order.setAddress("尚硅谷");
        //TODO 远程查询商品信息
        order.setProductList(Arrays.asList(product));

        return order;
    }

    //兜底方法
    public Order createOrderFallBack(Long productId, Long userId, BlockException e) {
        Order order = new Order();
        order.setId(0L);
        order.setTotalAmount(new BigDecimal("0"));
        order.setUserId(userId);
        order.setNickName("未知用户11111");
        order.setAddress("错误异常信息："+e.getClass());

        return order;
    }

    /**
     * 负载均衡调用
     * @param productId
     * @return
     */
    private Product getProductFromRemoteAnnotation(Long productId) {
        //获得商品服务所在的ip+端口
       /* List<ServiceInstance> instances = discoveryClient.getInstances("server-product");
        ServiceInstance serviceInstance = instances.get(0);
        //远程调用
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/product/" + productId;*/
        String url = "http://server-product/product/" + productId;
//        System.out.println("调用的远程服务地址是：url:" + url);
        //给请求发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }
    //远程调用1
    private Product getProductFromRemote(Long productId) {
        //获得商品服务所在的ip+端口
        List<ServiceInstance> instances = discoveryClient.getInstances("server-product");
        ServiceInstance serviceInstance = instances.get(0);
        //远程调用
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/product/" + productId;
        System.out.println("调用的远程服务地址是：url:" + url);
        //给请求发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }
}
